package com.cskaoyan.service;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.IssueVo;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class IssueServiceImpl implements IssueService {
    @Override
    public BaseRespVo list(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page, limit);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);


        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria marketIssueExampleCriteria = marketIssueExample.createCriteria();
        marketIssueExampleCriteria.andDeletedEqualTo(false);
        if (question != null && question != "") {
            marketIssueExampleCriteria.andQuestionLike("%" + question + "%");
        }
        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);

        // 来源于MyBatis的查询结果 ，使用PageInfo有参构造方法来封装这个PageInfo → 前面要使用OrderMapper做一个查询
        PageInfo pageInfo = new PageInfo(marketIssues);
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(CommonData.data(pageInfo));

    }

    @Override
    public BaseRespVo create(MarketIssue marketIssue) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);

        Date date = new Date();
        marketIssue.setAddTime(date);
        marketIssue.setUpdateTime(date);
        marketIssue.setDeleted(false);

        issueMapper.insertSelective(marketIssue);

        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria marketIssueExampleCriteria = marketIssueExample.createCriteria();

        marketIssueExampleCriteria.andQuestionEqualTo(marketIssue.getQuestion());
        marketIssueExampleCriteria.andAnswerEqualTo(marketIssue.getAnswer());
        // marketIssueExampleCriteria.andAddTimeEqualTo(date);

        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);
        MarketIssue issue = marketIssues.get(0);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(issue);

    }

    @SneakyThrows
    @Override
    public BaseRespVo update(IssueVo issueVo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTime = sdf.parse(issueVo.getAddTime());
        String answer = issueVo.getAnswer();
        String question = issueVo.getQuestion();
        Integer id = issueVo.getId();
        Boolean deleted = issueVo.getDeleted();

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);
        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(id);

        marketIssue.setAddTime(addTime);
        marketIssue.setUpdateTime(new Date());
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setDeleted(deleted);

        issueMapper.updateByPrimaryKey(marketIssue);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(marketIssue);
    }

    @Override
    public BaseRespVo delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);

        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(id);
        marketIssue.setDeleted(true);
        issueMapper.updateByPrimaryKey(marketIssue);

        sqlSession.commit();
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok();
    }


}
